﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Security.Cryptography;//引入命名空间，加密 
using System.IO;

namespace CommonLib
{
    public class DESencodingTxt
    {
        //默认密钥向量
        private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        /// <summary>
        /// 默认密钥
        /// </summary>
        private static string miyue = "88888888";

        /// <summary>
        /// 加密文本
        /// </summary>
        /// <param name="txt">需要加密的文本</param>
        /// <returns></returns>
        public static string DESencrypt(string txt)
        {
            if (string.IsNullOrEmpty(txt))
            {
                return "";
            }
            string chuliTxt = string.Empty;
            try
            {
                //string encryptKey加密密钥参数，我将该参数这里去掉了，用了默认密钥
                //byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbKey = Encoding.UTF8.GetBytes(miyue);//加密密钥 
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(txt);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                byte[] tembyte = mStream.ToArray();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < tembyte.Length; i++)
                {
                    sb.AppendFormat("{0:x2}", tembyte[i]);
                }
                chuliTxt = sb.ToString();
            }
            catch (Exception)
            {
                throw;
            }
            return chuliTxt;
        }

        /// <summary>
        /// 解密文本
        /// </summary>
        /// <param name="txt">需要解密的文本</param>
        /// <returns></returns>
        public static string DESDecoding(string txt)
        {
            if (string.IsNullOrEmpty(txt))
            {
                return "";
            }
            string chuliTxt = string.Empty;
            try
            {
                //string decryptKey解密密钥参数，我将该参数这里去掉了，用了默认密钥
                //byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbKey = Encoding.UTF8.GetBytes(miyue);//解密密钥 
                byte[] rgbIV = Keys;
                txt = txt.Replace(" ", "");
                txt = txt.Replace("\r\n", "");
                string txt16 = txt;
                byte[] inputByteArray = new byte[txt16.Length / 2];
                for (int i = 0; i < inputByteArray.Length; i++)
                {
                    inputByteArray[i] = Convert.ToByte(txt16.Substring(i * 2, 2), 16);
                }
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                chuliTxt = Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch (Exception)
            {
                throw;
            }
            return chuliTxt;
        }

    }
}
